<?php

class User extends CI_Controller{

	function __construct(){
		parent::__construct();
		$this->load->model('articles_model');
		$this->load->model('menu_model');
		$this->data['menuitems'] = $this->menu_model->menuitems;
		$this->data['isLoggedIn'] = $this->user_model->isLoggedIn();
	}

	//Display the profile of a user
	function profile(){
		$default = array('userid');
		$userinfo = $this->uri->uri_to_assoc(3, $default);

		$this->data['userid'] = $userinfo['userid'];
		$this->data['username'] = $this->user_model->getUsername($userinfo['userid']);
		$this->data['email'] = $this->user_model->getPublicEmail($userinfo['userid']);
		$this->data['openidURL'] = $this->user_model->getPublicOpenId($userinfo['userid']);

		$this->data['userarticles'] = $this->articles_model->getArticlesByUserId($userinfo['userid']);
		$this->data['bookmarkedarticles'] = $this->articles_model->getBookmarkedArticles($userinfo['userid']);
		$this->data['subscriptions'] = $this->articles_model->getSubscriptions($userinfo['userid']);
		
		$this->data['isSessionUser'] = $this->user_model->sessionUser($userinfo['userid']);

		$this->data['main_content'] =  'user_profile_view';
		$this->data['selected_item'] = $this->menu_model->getMenuItem('home');
		$this->load->view('includes/template', $this->data);
	}
	
	//Display the settings that a logged in user can change
	function settings(){
		$default = array('userid');
		$userinfo = $this->uri->uri_to_assoc(3, $default);
		//should only be accessible when logged in and only access yout own settings
		if($this->user_model->isLoggedIn() && $this->user_model->sessionUser($userinfo['userid'])){

			$this->data['userid'] = $userinfo['userid'];
			$this->data['username'] = $this->user_model->getUsername($userinfo['userid']);
			$this->data['email'] = $this->user_model->getEmail($userinfo['userid']);
			$this->data['openidURL'] = $this->user_model->getOpenId($userinfo['userid']);
				
			$this->data['public_openid'] = $this->user_model->isOpenIdPublic($userinfo['userid']);
			$this->data['public_email'] = $this->user_model->isEmailPublic($userinfo['userid']);

			$this->data['main_content'] =  'user_settings_view';
			$this->data['selected_item'] = $this->menu_model->getMenuItem('home');
			$this->load->view('includes/template', $this->data);
		}
		else{
			$this->data['main_content'] =  'no_access_view';
			$this->data['selected_item'] = $this->menu_model->getMenuItem('home');
			$this->load->view('includes/template', $this->data);
		}
	}
	
	//updates the users settings in the database
	function updateSettings(){
		$default = array('userid');
		$userinfo = $this->uri->uri_to_assoc(3, $default);

		if($this->user_model->isLoggedIn() && $this->user_model->sessionUser($userinfo['userid'])){
			$this->load->library('form_validation');
			$this->form_validation->set_rules('username', 'Username', 'required');
			$this->form_validation->set_rules('email', 'Email', 'required|valid_email');

			if($this->form_validation->run() == TRUE){
				$username = $this->input->post('username');
				$email = $this->input->post('email');
				$public_openid = $this->input->post('public_openid');
				$public_email = $this->input->post('public_email');

				if($public_openid == 'public'){
					$public_openid = 1;
				}else{
					$public_openid = 0;
				}

				if($public_email == 'public'){
					$public_email = 1;
				}else{
					$public_email = 0;
				}

				$this->user_model->updateSettings($userinfo['userid'], $username, $email, $public_openid, $public_email);
				
				redirect('/user/settings/userid/'.$userinfo['userid'], 'location');
			}
			else{
				if($this->user_model->isLoggedIn() && $this->user_model->sessionUser($userinfo['userid'])){

					$this->data['userid'] = $userinfo['userid'];
					$this->data['username'] = $this->user_model->getUsername($userinfo['userid']);
					$this->data['email'] = $this->user_model->getEmail($userinfo['userid']);
					$this->data['openidURL'] = $this->user_model->getOpenId($userinfo['userid']);
						
					$this->data['public_openid'] = $this->user_model->isOpenIdPublic($userinfo['userid']);
					$this->data['public_email'] = $this->user_model->isEmailPublic($userinfo['userid']);

					$this->data['main_content'] =  'user_settings_view';
					$this->data['selected_item'] = $this->menu_model->getMenuItem('home');
					$this->load->view('includes/template', $this->data);
				}
				else{
					$this->data['main_content'] =  'no_access_view';
					$this->data['selected_item'] = $this->menu_model->getMenuItem('home');
					$this->load->view('includes/template', $this->data);
				}
			}
		}
		else{
			$this->data['main_content'] =  'no_access_view';
			$this->data['selected_item'] = $this->menu_model->getMenuItem('home');
			$this->load->view('includes/template', $this->data);
		}
	}
}